
Contents
1 Introduction 4
2 Object Oriented Programming – Brief Introduction 5
3 Classes and instances 6
3.1 Example: Java’s String class . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Example: String object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Standard way of instantiatiation . . . . . . . . . . . . . . . . . . . . . . . 7
4 Class definition and instantiation 8
4.1 Defining classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 Adding instance variables . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.3 Defining methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.4 Example - Defining a class . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.5 Declaration and instantiation . . . . . . . . . . . . . . . . . . . . . . . . 10
4.6 Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.7 Declaration and instantiation . . . . . . . . . . . . . . . . . . . . . . . . 10
4.8 Adding method to a class . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.9 The dot (.) operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.10 Are there any default values? . . . . . . . . . . . . . . . . . . . . . . . . . 13
5 Getters and setters 14
5.1 Bad client, bad bad client! . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.2 Changing visibility to private . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.3 How does one access (read/write) private instance variables . . . . . . 15
5.4 Setters must provide validation where applicable . . . . . . . . . . . . . 15
5.5 I wish creating objects was easier . . . . . . . . . . . . . . . . . . . . . . 18
6 Constructors 19
6.1 Example - Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2 Constructors should call setters - always! . . . . . . . . . . . . . . . . . . 20
6.3 Default constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.4 Defining the default constructor . . . . . . . . . . . . . . . . . . . . . . . 20
2